package com.luo.service.comment.impl;

import com.luo.dao.CommentDao;
import com.luo.pojo.CommentsInfo;
import com.luo.pojo.CommentsReply;
import com.luo.service.comment.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("CommentService")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    @Override
    public List<CommentsInfo> queryInfoComments(String ownerId) {
        return this.commentDao.queryInfoComments(ownerId);
    }

    @Override
    public List<CommentsReply> queryReplyComments(String ownerId) {
        return this.commentDao.queryReplyComments(ownerId);
    }

    @Override
    public boolean isDeletedF(int id,String createTime) {
        try{
            this.commentDao.deleteCommentByFather(id,createTime);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean isDeletedR(int commentId, String createTime) {
        try{
            this.commentDao.deleteCommentByReply(commentId,createTime);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<CommentsInfo> getOwnersAllComments(String ownerId) {
        List<CommentsInfo> commentsInfos = this.commentDao.queryInfoComments(ownerId);
        List<CommentsReply> commentsReplies = commentDao.queryReplyComments(ownerId);
        for (CommentsInfo commentsInfo : commentsInfos) {
            List<CommentsReply> bufferReply = new ArrayList<>();
            for (CommentsReply commentsReply : commentsReplies) {
                if (commentsInfo.getId() == Integer.parseInt(commentsReply.getCommentId())) {
                    bufferReply.add(commentsReply);
                }
                commentsInfo.setListReply(bufferReply);
            }
        }
        return commentsInfos;
    }

    @Override
    public boolean isInsertedF(int type, String ownerId, int fromId, String fromName, String fromAvatar, String content) {
        try{
            this.commentDao.insertCommentByFather(type,ownerId,fromId,fromName,fromAvatar,content);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean isInsertedR(int commentId, int fromId, String fromName, String fromAvatar, int toId, String toName, String toAvatar, String content) {
        try{
            this.commentDao.insertCommentByReply(commentId,fromId,fromName,fromAvatar,toId,toName,toAvatar,content);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public List<CommentsInfo> getReplyPersonInfo(String usename) {
        return this.commentDao.getReplyPerson(usename);
    }
}
